package com.looookme.shaozhuo.datasave;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;


/**
 * Created by Shaozhuo on 2015/9/11.
 */
public class Data_SqlLiteOpenHelper extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initView();
    }

    private void initView() {
        LinearLayout ll = new LinearLayout(this);
        ll.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        setContentView(ll);

        Button getBtn = new Button(this);
        getBtn.setText("Get Button");
        getBtn.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        getBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DBOpenHelper helper = new DBOpenHelper(Data_SqlLiteOpenHelper.this, "myDb.db");
                SQLiteDatabase db = helper.getReadableDatabase();

                Cursor c = db.query("tb_user", new String[]{"name", "sex"}, null, null, null, null, null, "40");
                if (c != null) {
                    while (c.moveToNext()) {
                        Log.i("SSSSSS", c.getString(c.getColumnIndex("name")) + " - " + " - " + c.getString(c.getColumnIndex("sex")));
                    }
                    c.close();
                }
                db.close();
            }
        });
        ll.addView(getBtn);
    }
}
